/*
 * @(#)XMLBuilder.java 1.0 2006-8-15
 * Copyright 2006 VandaGroup, Inc. All rights reserved.
 */
package com.vanda.ajax;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * Ajax生成xml类. <br>
 * 
 * @author 王瑛<br>
 * @version Version 1.00<br>
 */
public class XMLBuilder {
	/** Property标签名称 */
    private static String ELEMENT_PROPERTY = "p";
    /** 映射对标签名称 */
    private static String ELEMENT_MAP = "m";
    /** 数组名标签名称 */
    private static String ELEMENT_ARRAY = "a";
    /** 数组内容标签名称 */
    private static String ELEMENT_ARRAY_DATA = "ad";
    /** 错误信息标签名称 */
    private static String ELEMENT_ERROR = "e";
    /** 提示信息标签名称 */
    private static String ELEMENT_MESSAGE = "me";
    /** 确认信息标签名称 */
    private static String ELEMENT_CONFIRM = "cf"; //确认信息add by chenlei 20060914
    /** Attribute标签名称 */
    private static String ATTRIBUTE_KEY = "k";
    
    public static String NEW_LINE = "#$#";

    /** 保存Key值 */
    List keys = new ArrayList();
    /** 保存values */
    List values = new ArrayList();

    /** 保存映射对的Key值 */
    List mKeys = new ArrayList();
    /** 保存映射对的Value值 */
    List mValues = new ArrayList();

    /** 错误消息 */
    String errorMsg = null;
    /** 提示消息 */
    String promptMsg = null;
    /** 确认消息 */
    String confirmMsg = null;

    public void setErrorMsg(String msg) {
        errorMsg = msg;
    }

    public void setPromptMsg(String msg) {
        promptMsg = msg;
    }

    //确认信息add by chenlei 20060914
    public void setConfirmMsg(String msg) {
        confirmMsg = msg;
    }

    public void addProperty(String key, String value) {
        keys.add(key);
        values.add(value);
    }

    public void addArray(String key, String[] valueAry) {
        keys.add(key);
        values.add(valueAry);
    }

    public void addMap(String key, String value) {
        mKeys.add(key);
        mValues.add(value);
    }

    /**
     * 根据设置内容生成xml
     * @return xml格式的字符串
     */
    public String createXML() {
        String xml = null;

        SAXReader saxReader = new SAXReader();

        DocumentFactory factory = saxReader.getDocumentFactory();
        Document document = factory.createDocument();

        Element root = document.addElement("ajax");

        //设置错误信息
        if (errorMsg != null) {
            Element error = root.addElement(ELEMENT_ERROR);
            error.setText(errorMsg);
        }
        //设置提示信息
        if (promptMsg != null) {
            Element prompt = root.addElement(ELEMENT_MESSAGE);
            prompt.setText(promptMsg);
        }

        //设置确认信息add by chenlei 20060914
        if (confirmMsg != null) {
            Element confirm = root.addElement(ELEMENT_CONFIRM);
            confirm.setText(confirmMsg);
        }

        //设置页面控件value
        for (int i = 0; i < keys.size(); i++) {
            Object obj = values.get(i);
            
            if(obj == null){
            	addPropertyElement(root, (String) keys.get(i), "");
            }
            if (obj instanceof String) {
                addPropertyElement(root, (String) keys.get(i), (String) obj);
            } else if (obj instanceof String[]) {
                addArrayElement(root, (String) keys.get(i), (String[]) obj);
            }

        }

        for (int i = 0; i < mKeys.size(); i++) {
            addMapElement(root, (String) mKeys.get(i), (String) mValues.get(i));
        }

        return document.asXML();
    }

    /** 
     * 生成数组标签
     * @param root xml头结点
     * @param key 数组Key值
     * @param value 数组Value值
     */
    private void addArrayElement(Element root, String key, String[] value) {
        Element property = root.addElement(ELEMENT_ARRAY);

        property.addAttribute(ATTRIBUTE_KEY, key);

        for (int i = 0; i < value.length; i++) {
            Element sub = property.addElement(ELEMENT_ARRAY_DATA);
            sub.setText(value[i]);
        }

    }

    /** 
     * 生成页面属性标签
     * @param root xml头结点
     * @param key 页面属性Key值
     * @param value 页面属性Value值
     */
    private void addPropertyElement(Element root, String key, String value) {
        Element property = root.addElement(ELEMENT_PROPERTY);

        property.addAttribute(ATTRIBUTE_KEY, key);

        property.setText(value);

    }

    /**
     * 生成映射对属性标签 
     * @param root xml头结点
     * @param key 映射对Key值
     * @param value 映射对Value值
     */
    private void addMapElement(Element root, String key, String value) {
        Element property = root.addElement(ELEMENT_MAP);

        property.addAttribute(ATTRIBUTE_KEY, key);

        property.setText(value);

    }

    /*public static void main(String[] args) {
        XMLBuilder builder = new XMLBuilder();
        try {
            builder.addProperty("name1", "eagle");
            builder.addProperty("sex", "1");
            builder.addArray("percent", new String[] { "100", "85" });

            LogFunc.info(builder.createXML());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }*/
}
